interface MessageList {
    [type: string]: Array<FunctionWithAlis>
}
interface FunctionWithAlis {
    alias: string,
    fn: Function
}
class Observe {
    private messageList: MessageList = {};
    register(type: string, fn: Function, alias: string = '') {
        let item = this.messageList[type];
        if (item) {
            item.push({ fn, alias });
        } else {
            this.messageList[type] = [{ fn, alias }];
        }
    }
    fire(type: string, _this: Object = window, ...args: any) {
        let item = this.messageList[type];
        if (item) {
            for (let i = 0; i < item.length; i++) {
                item[i].fn.apply(_this, args);
                console.log(item[i].fn.toString());
            }
        }
    }
    log(type: string, _this: Object = window, ...args: any) {
        let item = this.messageList[type];
        if (item) {
            for (let i = 0; i < item.length; i++) {
                console.log(item[i].fn.toString());
            }
        }
    }
    remove(type: string, cb: Function = undefined, alias: string = '') {
        let item = this.messageList[type];
        if (item) {
            for (let i = item.length - 1; i >= 0; i--) {
                (item[i].alias === alias || item[i].fn === cb) && item.splice(i, 1)
            }
        }
    }
    getLen(type: string) {
        return this.messageList[type] && this.messageList[type].length || 0
    }
}
interface DataCallback {
    (newValue: any, oldValue: any): void
}
class defineReactive {
    private dataList = new Observe();
    constructor(private obj: Object, datas: Array<string>) {
        let that = this;
        for (let i = 0; i < datas.length; i++) {
            Object.defineProperty(obj, datas[i], {
                set(v) {
                    that.dataList.fire(datas[i], v, this.value);
                    this.value = v;
                }
            })

        }
    }
    addCb(data: string, cb: DataCallback) {
        this.dataList.register(data, cb)
    }
}
class Stack<T>{
    private arr: Array<T>;
    private len = -1;
    constructor() {
        this.arr = [];
    }
    getItem() {
        if (this.len == -1) {
            throw Error("栈为空");
        }
        return this.arr[this.len];
    }
    pushItem(item: T) {
        this.arr.push(item)
        this.len++;
        return this.len;
    }
    popItem() {
        if (this.len == -1) {
            throw Error("栈为空")
        }
        this.len--;
        let res = this.arr.pop();
        return res;
    }
}
export { Stack, Observe };